package com.gxa.controller;

import com.gxa.BusinessException;
import com.gxa.pojo.InStorage;
import com.gxa.service.InStorageService;
import com.gxa.utils.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author 陈静
 * @date 2020/9/21 18:36
 */

@Controller
@RequestMapping("/inStorage")
@CrossOrigin // 解决跨域
public class InStorageController {
    @Resource
    InStorageService inStorageService;

    @ResponseBody
    @RequestMapping(value = "/findSupplierName",method = RequestMethod.POST)
    /**
     * 查询所有供应商名字
     * @return 供应商名字
     */
    public Map<String, Object> findSupplierName() throws BusinessException {
        List<Map<String,Object>>  supplierName = inStorageService.findSupplierName();
        JsonResult jsonResult = new JsonResult();
        if(supplierName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(supplierName);
            jsonResult.setMsg("success");
        }else{
           throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping(value = "/findStoreName",method = RequestMethod.POST)
    /**
     * 查询所有仓库名字
     * @return 供仓库名字
     */
    public Map<String, Object> findStoreName() throws BusinessException {
        List<Map<String,Object>> storeName = inStorageService.findStoreName();
        JsonResult jsonResult = new JsonResult();
        if(storeName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(storeName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    //使用Map去获取任意数量，任意名称的前端数据
    //依然使用@RequestBody注入，且前端依然配置 post+contentTye

    /**
     * 根据时间或者供货厂商或者进货仓库或者审核状态查询
     * @param map
     * @return 入库信息
     */
    @ResponseBody
    @RequestMapping(value = "/findInStorage",method = RequestMethod.POST)
    public Map findInStorage(@RequestBody(required = false) Map map)  {
            JsonResult jsonResult = new JsonResult();
            String  number= map.get("number").toString();
            String size= map.get("size").toString();
            Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
            Integer limit=Integer.parseInt(size);
            map.put("offset",offset);
            map.put("limit",limit);
            String startDate = map.get("startDate").toString();
             String endDate = map.get("endDate").toString();
            if ( (startDate!= null && !"".equals(startDate))  && (endDate!= null && !"".equals(endDate))) {
                Timestamp startDate2 = Timestamp.valueOf(startDate);
                Timestamp endDate2 = Timestamp.valueOf(endDate);
                map.put("startDate2", startDate2);
                map.put("endDate2", endDate2);
                List<InStorage> inStorage = inStorageService.findInStorage(map);
                Integer integer = inStorageService.countFindInStorage(map);
                jsonResult.setData(inStorage);
                jsonResult.put("count",integer);
                jsonResult.setCode("200");
                jsonResult.setMsg("success");
            }else {
                List<InStorage> inStorage = inStorageService.findInStorage(map);
                Integer integer = inStorageService.countFindInStorage(map);
                jsonResult.setData(inStorage);
                jsonResult.put("count",integer);
                jsonResult.setCode("200");
                jsonResult.setMsg("success");
            }


        return jsonResult.getValues();
    }

    // number 页码    size：每页显示记录数

    /**
     *
     * @param map 起始页 每一页大小
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/inStoragePage",method = RequestMethod.POST)
    public Map findAllInStorageByPage(@RequestBody Map map){
        String  number= map.get("number").toString();
        String size= map.get("size").toString();
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
       Integer limit=Integer.parseInt(size);
        List<InStorage> inStorages = inStorageService.findAllInStorageByPage(offset, limit);
        Integer count = inStorageService.findAllInStorage(); // select count(*) from
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(inStorages);
        jsonResult.put("count",count);
        return jsonResult.getValues();
    }

    /**
     *  修改审核状态
     * @param map id
     * @return  返回影响行数
     */
    @ResponseBody
    @RequestMapping(value = "/updateApproval",method = RequestMethod.POST)
    public Map updateApproval(@RequestBody Map map) throws BusinessException {
        Integer line = inStorageService.updateState(map);
        JsonResult jsonResult = new JsonResult();
        if(line >0){
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }else {
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     *  查询所有品牌名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBrandName",method = RequestMethod.POST)
    public Map<String,Object> findBrandName() throws BusinessException {
        List<Map<String, Object>> brandName = inStorageService.findBrandName();
        JsonResult jsonResult = new JsonResult();
        if(brandName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(brandName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     *  查询所有分类名称
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findLevelName",method = RequestMethod.POST)
    public Map<String,Object> findLevelName() throws BusinessException {
        List levelName = inStorageService.findLevelName();
        JsonResult jsonResult = new JsonResult();
        if(levelName != null) {
            jsonResult.setCode("200");
            jsonResult.setData(levelName);
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }
    /**
     *  查询对应的入库信息
     * @param map id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findInStorageById",method = RequestMethod.POST)
    public Map findInStorageById(@RequestBody Map map){
        InStorage inStorage = inStorageService.findInStorageById(Integer.parseInt(map.get("id").toString()));
        List<Map<String, Object>> inStorageGoods = inStorageService.findInStorageGoodsByInStorageId(Integer.parseInt(map.get("id").toString()));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("inStorage",inStorage);
        jsonResult.put("inStorageGoods",inStorageGoods);
        jsonResult.setMsg("success");
        return jsonResult.getValues();
    }

    /**
     * 删除对应的入库信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteInStorageById",method = RequestMethod.POST)
    public Map deleteInStorageById(@RequestBody Map map) throws BusinessException {
        Integer goodsLines = inStorageService.deleteInStorageGoodsByInStorageId(Integer.parseInt(map.get("id").toString()));
        Integer inStorageLines = inStorageService.deleteInStorageById(Integer.parseInt(map.get("id").toString()));
        JsonResult jsonResult = new JsonResult();
        if( goodsLines<0 && inStorageLines<0){
            throw new BusinessException("系统异常，请稍后再试！");
        }else {
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }
        return jsonResult.getValues();
    }

    /**
     *  插入入库信息和商品信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertInStorage",method =RequestMethod.POST )
    public Map insertInStorage(@RequestBody Map<String,Object> map) throws BusinessException {
        Integer integer = inStorageService.insertInStorage(map);
        JsonResult jsonResult = new JsonResult();
        if(integer>-1) {
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }
        return jsonResult.getValues();
    }

    /**
     * 根据名称 品牌和分类查询入库商品信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findInStorageGoods",method = RequestMethod.POST)
    public Map<String,Object> findInStorageGoods(@RequestBody(required = false) Map<String,Object> map)  {

        String  number= map.get("number").toString();
        String size= map.get("size").toString();
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        map.put("offset",offset);
        map.put("limit",limit);
        List<Map<String, Object>> outStorageGoods = inStorageService.findInStorageGoods(map);
        Integer integer = inStorageService.countFindInStorageGoods(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(outStorageGoods);
        jsonResult.setCode("200");
        jsonResult.setMsg("success");
        jsonResult.put("count",integer);
        return jsonResult.getValues();
    }

    /**
     * 修改入库信息
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateInStorage",method = RequestMethod.POST)
    public Map updateInStorage(@RequestBody Map map) throws BusinessException {
        Integer integer = inStorageService.updateInStorage(map);
        JsonResult jsonResult = new JsonResult();
        if(integer>-1){
            jsonResult.setCode("200");
            jsonResult.setMsg("success");
        }else{
            throw new BusinessException("系统异常，请稍后再试！");
        }

        return jsonResult.getValues();
    }

}
